using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Erebos.GameScreenFiles;

namespace Erebos
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MainGame : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private KeyboardState _oldState;

        private static Screen _screen;

        public static Texture2D ErebosLogo;
        public static Dictionary<string, Texture2D> Textures = new Dictionary<string, Texture2D>();
        public static Effect Effect;
        public static Dictionary<string, SpriteFont> Fonts = new Dictionary<string, SpriteFont>();
        public static Dictionary<string, Song> Songs = new Dictionary<string, Song>();
        public static bool Close;
        private static ContentManager _contentManager;
        public static bool IsActive = true;
        public static Game Game;

        public static IServiceProvider ServiceProvider
        {
            get { return _contentManager.ServiceProvider; }
        }

        public static ContentManager GetContentManager()
        {
            return _contentManager;
        }

        public static T LoadContent<T>(string asset)
        {
           return _contentManager.Load<T>(asset);
        }

        public static void SetScreen(Screen s)
        {
            _screen = s;
        }

        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            _contentManager = Content;

            IsMouseVisible = true;
            Game = this;
            ClientHandler.Connect();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            /*graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height; //Default: 480
            graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;   //Default: 800
            graphics.IsFullScreen = true;
            graphics.ApplyChanges();*/

            ScreenSizeHelper.SizeHeight = graphics.PreferredBackBufferHeight;
            ScreenSizeHelper.SizeWidth = graphics.PreferredBackBufferWidth;
            ScreenSizeHelper.Device = GraphicsDevice;

            base.Initialize();
        }

        /// <summary>
        /// LoadGameContent will be load all the content which isn't needed befor login.
        /// </summary>
        public static void LoadGameContent()
        {
            Textures.Add("Box/LeftTopCorner", _contentManager.Load<Texture2D>("Box\\LeftTopCorner"));
            Textures.Add("Box/TopSide", _contentManager.Load<Texture2D>("Box\\TopSide"));
            Textures.Add("Box/RightTopCorner", _contentManager.Load<Texture2D>("Box\\RightTopCorner"));
            Textures.Add("Box/RightSide", _contentManager.Load<Texture2D>("Box\\RightSide"));
            Textures.Add("Box/RightButtomCorner", _contentManager.Load<Texture2D>("Box\\RightButtomCorner"));
            Textures.Add("Box/ButtomSide", _contentManager.Load<Texture2D>("Box\\ButtomSide"));
            Textures.Add("Box/LeftButtomCorner", _contentManager.Load<Texture2D>("Box\\LeftButtomCorner"));
            Textures.Add("Box/LeftSide", _contentManager.Load<Texture2D>("Box\\LeftSide"));
            Textures.Add("Rang/Admin", _contentManager.Load<Texture2D>("Rang\\Admin"));
            Textures.Add("Rang/Bote", _contentManager.Load<Texture2D>("Rang\\Bote"));
            Textures.Add("Rang/Mod", _contentManager.Load<Texture2D>("Rang\\Mod"));
            Textures.Add("Rang/SuperBote", _contentManager.Load<Texture2D>("Rang\\SuperBote"));
            Textures.Add("Box/Fill", _contentManager.Load<Texture2D>("Box\\Fill"));
            Textures.Add("Strich250", _contentManager.Load<Texture2D>("Strich250"));
            Textures.Add("TmpMensch", _contentManager.Load<Texture2D>("tmpMensch"));
            Textures.Add("PlayButton", _contentManager.Load<Texture2D>("PlayButton"));
            Textures.Add("HeightMap", _contentManager.Load<Texture2D>("HeightMap"));
            Textures.Add("Sand", _contentManager.Load<Texture2D>("Sand"));
            Textures.Add("Grass", _contentManager.Load<Texture2D>("Grass"));
            Textures.Add("Rock", _contentManager.Load<Texture2D>("Rock"));
            Textures.Add("Snow", _contentManager.Load<Texture2D>("Snow"));
            Textures.Add("FloorHerringbone", _contentManager.Load<Texture2D>("FloorHerringbone"));

            Textures.Add("Overlay/InventarButton", _contentManager.Load<Texture2D>("Overlay/InventarButton"));
            Textures.Add("Overlay/MenuButton", _contentManager.Load<Texture2D>("Overlay/MenuButton"));
            Textures.Add("Overlay/HotKeyBarLeft", _contentManager.Load<Texture2D>("Overlay/HotKeyLeft"));
            Textures.Add("Overlay/HotKeyBarMiddle", _contentManager.Load<Texture2D>("Overlay/HotKeyMiddle"));
            Textures.Add("Overlay/HotKeyBarRight", _contentManager.Load<Texture2D>("Overlay/HotKeyRight"));
            Textures.Add("Overlay/HotKeys", _contentManager.Load<Texture2D>("Overlay/HotKeys"));
            Textures.Add("Overlay/Lebensleiste", _contentManager.Load<Texture2D>("Overlay/Lebensleiste"));
            Textures.Add("Overlay/Ausdauerleiste", _contentManager.Load<Texture2D>("Overlay/Ausdauerleiste"));
            Effect = _contentManager.Load<Effect>("effect");
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            ErebosLogo = Content.Load<Texture2D>("ErebosLogo");
            Fonts.Add("Arial16", Content.Load<SpriteFont>("MenuFont"));
            Fonts.Add("Arial18Bold", Content.Load<SpriteFont>("CreditFont"));
            Fonts.Add("Arial24Bold", Content.Load<SpriteFont>("BigFont"));
            Fonts.Add("Arial10", Content.Load<SpriteFont>("HinweisFont"));
            Fonts.Add("Arial12", Content.Load<SpriteFont>("NormalFont"));
            Textures.Add("Blood", Content.Load<Texture2D>("Blood"));
            Textures.Add("TextBox", Content.Load<Texture2D>("TextBox"));
            Textures.Add("Button", Content.Load<Texture2D>("Button"));
            Textures.Add("Box", Content.Load<Texture2D>("Box"));
            Textures.Add("Zahnrad", Content.Load<Texture2D>("Zahnrad"));
            Songs.Add("Final Count", Content.Load<Song>("music\\Final Count"));

            LoadGameContent();

            _screen = new GameScreenFiles.GameScreen(new Charackter("Test", 1, 1, "22222", 1));
            //_screen = new TrittEinScreenFiles.TrittEinScreen();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.F10) && _oldState.IsKeyUp(Keys.F10))
            {
                if (!graphics.IsFullScreen)
                {
                    graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height; //Default: 480
                    graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;   //Default: 800
                    graphics.IsFullScreen = true;
                }
                else
                {
                    graphics.PreferredBackBufferHeight = 480; //Default: 480
                    graphics.PreferredBackBufferWidth = 800;  //Default: 800
                    graphics.IsFullScreen = false;
                }
                graphics.ApplyChanges();
                ScreenSizeHelper.SizeHeight = graphics.PreferredBackBufferHeight;
                ScreenSizeHelper.SizeWidth = graphics.PreferredBackBufferWidth;
                ScreenSizeHelper.Device = GraphicsDevice;
                Camera.Device = GraphicsDevice;
                Camera.ChangeScreenSize();
            }

            _oldState = Keyboard.GetState();

            if (Close)
                this.Exit();

            IsActive = base.IsActive;

            // TODO: Add your update logic here
            if (_screen != null)
                _screen.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            if (_screen != null)
                _screen.DrawBefore(gameTime);

            base.Draw(gameTime);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            if (_screen != null)
                _screen.Draw(spriteBatch, gameTime);

            spriteBatch.End();
        }
    }
}
